package com.common.platform.sys.exception.aop;


import com.common.platform.auth.context.LoginContextHolder;
import com.common.platform.auth.exception.AuthException;
import com.common.platform.auth.exception.AuthExceptionEnum;
import com.common.platform.auth.exception.PermissionException;

import com.common.platform.base.config.mail.MailUtil;
import com.common.platform.base.exception.BizExceptionEnum;
import com.common.platform.base.exception.ServiceException;
import com.common.platform.base.utils.CoreUtil;
import com.common.platform.sys.base.controller.response.ErrorResponseData;
import com.common.platform.sys.exception.InvalidKaptchaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import static com.common.platform.base.config.context.HttpContext.getRequest;


@ControllerAdvice
@Order(-100)
public class GlobalExceptionHandler {

    public Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MailUtil mailUtil;

    /**
     * 认证异常--认证失败（账号密码错误，账号被冻结，token过期等）
     */

    @ExceptionHandler(AuthException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ErrorResponseData unAuth(AuthException e) {
        return new ErrorResponseData(e.getCode(), e.getMessage());
    }


    /**
     * 认证异常--没有访问权限
     */
    @ExceptionHandler(PermissionException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public ErrorResponseData permissionExpection(PermissionException e) {
        return new ErrorResponseData(e.getCode(), e.getMessage());
    }

    /**
     * 认证异常--验证码错误异常
     */
    @ExceptionHandler(InvalidKaptchaException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorResponseData credentials(InvalidKaptchaException e) {
        String username = getRequest().getParameter("username");
        //TODO 保存日志
        return new ErrorResponseData(AuthExceptionEnum.VALUE_CODE_ERROR.getcode(), AuthExceptionEnum.VALUE_CODE_ERROR.getmessage());
    }

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorResponseData bussiness(ServiceException e) {
        if (LoginContextHolder.getContext().hasLogin()) {
            //TODO 保存日志
        }
        getRequest().setAttribute("tip", e.getMessage());
        logger.error("业务异常:", e);
        return new ErrorResponseData(e.getCode(200), e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public ErrorResponseData notFount(RuntimeException e) {
        if (LoginContextHolder.getContext().hasLogin()) {
            //TODO 保存日志
        }
        getRequest().setAttribute("tip", "服务器未知运行时异常");
        logger.error("运行时异常:", e);
        //给管理员发送异常日志邮件
        mailUtil.send("运行时异常:", CoreUtil.getExceptionMsg(e));
        return new ErrorResponseData(BizExceptionEnum.SERVER_ERROR.getcode(), BizExceptionEnum.SERVER_ERROR.getmessage());
    }


}
